"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : PACKAGE
Case Name   : 强制删除有package对象的用户后调用package
Create At   : 2022/04/27
Owner       : opentestcase004
Description :
    1.创建用户
    2.新建用户声明并定义package
    3.强制删除新建用户
    4.调用package
Expect      :
    1.成功
    2.成功
    3.成功
    4.合理报错
History     :
"""

import os
import unittest
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class PACKAGE(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.Con = Constant()
        self.user = 'u_package_0063'
        self.package = 'p_test_0063'
        self.procedure = 'p_package_0063'

    def test_package(self):
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        step = 'step1:创建用户 expect:成功'
        self.logger.info(step)
        create_user = self.primary_sh.execut_db_sql(f'''
            drop user if exists {self.user} cascade;
            create user {self.user} with password '{macro.PASSWD_REPLACE}';''')
        self.logger.info(create_user)
        self.assertIn(self.Con.CREATE_ROLE_SUCCESS_MSG, create_user,
                      '执行失败' + step)

        step = 'step2:新建用户声明并定义package expect:成功'
        self.logger.info(step)
        spec_package = self.primary_sh.execut_db_sql(
            f'''create or replace package {self.package} is
            procedure {self.procedure}();
            end {self.package};

            create or replace package body {self.package} is
            procedure {self.procedure}()
            is
                v_int int;
            begin
                v_int := (select 1+2);
                raise info 'v_int:%',v_int;
            end;
            end {self.package};''',
            sql_type=f" -U {self.user} -W '{macro.PASSWD_REPLACE}'")
        self.logger.info(spec_package)
        self.assertIn(self.Con.create_package_success_msg, spec_package,
                      '执行失败' + step)
        self.assertIn(self.Con.create_package_body_success_msg, spec_package,
                      '执行失败' + step)

        step = 'step3:强制删除新建用户 expect:成功'
        self.logger.info(step)
        drop_user = self.primary_sh.execut_db_sql(
            f'''drop user {self.user} cascade;''')
        self.logger.info(drop_user)
        self.assertIn(self.Con.DROP_ROLE_SUCCESS_MSG, drop_user,
                      '执行失败' + step)

        step = 'step4:调用package expect:合理报错'
        self.logger.info(step)
        invoke_pac = self.primary_sh.execut_db_sql(
            f'''set current_schema = {self.user};
            call {self.package}.{self.procedure}();''')
        self.logger.info(invoke_pac)
        self.assertIn('SET', invoke_pac, '执行失败' + step)
        self.assertIn(f'ERROR:  schema "{self.package}" does not exist',
                      invoke_pac, '执行失败' + step)

    def tearDown(self):
        self.logger.info('-----无需清理环境-----')
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
